package com.sky.service.impl;

import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.sky.constant.MessageConstant;
import com.sky.constant.StatusConstant;
import com.sky.dto.SetmealDTO;
import com.sky.dto.SetmealPageQueryDTO;
import com.sky.entity.Setmeal;
import com.sky.entity.SetmealDish;
import com.sky.exception.DeletionNotAllowedException;
import com.sky.mapper.CategoryMapper;
import com.sky.mapper.SetmealDishMapper;
import com.sky.mapper.SetmealMapper;
import com.sky.result.PageResult;
import com.sky.service.SetmealService;
import com.sky.vo.DishItemVO;
import com.sky.vo.SetmealVO;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;
@Service
public class SetmealServiceImpl implements SetmealService {

    @Autowired
    private SetmealMapper setmealMapper;

    @Autowired
    private SetmealDishMapper setmealDishMapper;

    @Autowired
    private CategoryMapper categoryMapper;

    @Transactional
    @Override
    public void save(SetmealDTO setmealDTO) {
       /*


        新增套餐

        */
        Setmeal setmeal = new Setmeal();
        BeanUtils.copyProperties(setmealDTO,setmeal);
        setmealMapper.save(setmeal);
        Long id = setmeal.getId();

        /*在套餐菜品表中添加*/
        List<SetmealDish> setmealDishes = setmealDTO.getSetmealDishes();
        if(setmealDishes!=null&&setmealDishes.size()>0){
            setmealDishes.forEach(setmealDish -> {
                setmealDish.setSetmealId(id);
            });
            //表中插入n个dish
            setmealDishMapper.save(setmealDishes);
        }

    }

    @Override
    public PageResult pageQuery(SetmealPageQueryDTO setmealPageQueryDTO) {
        PageHelper.startPage(setmealPageQueryDTO.getPage(),setmealPageQueryDTO.getPageSize());
        Page<SetmealVO> page = setmealMapper.pageQuery(setmealPageQueryDTO);
        return new PageResult(page.getTotal(),page.getResult());
    }

    @Transactional
    @Override
    public void delete(List<Integer> ids) {
        for (Integer id : ids) {
            Setmeal setmeal = new Setmeal();
          if(setmeal.getStatus()== StatusConstant.ENABLE){
              throw new DeletionNotAllowedException(MessageConstant.DISH_ON_SALE);
          }
        }

        //删除套餐后，对应套餐的菜品也需要删除
        for (Integer id : ids) {
            setmealMapper.delete(id);
            setmealDishMapper.delete(id);
        }
    }

    /*根据id查询套餐*/
    @Override
    public SetmealVO getById(Integer id) {

        SetmealVO setmealVO = setmealMapper.getById(id);
        //查询分类名
        setmealVO.setCategoryName(categoryMapper.getCategoryNameBySetmealId(id));


        //查询套餐对应菜品
        setmealVO.setSetmealDishes(setmealDishMapper.getDishBySetmealId(id));
        return setmealVO;
    }


    /*套餐的起售的禁售*/
    @Override
    public void startOrStop(Integer status, Integer id) {
        setmealMapper.startOrStop(status,id);
    }


    /*套餐的更新*/
    @Override
    public void update(SetmealDTO setmealDTO) {
        Setmeal setmeal = new Setmeal();
        BeanUtils.copyProperties(setmealDTO,setmeal);
        setmealMapper.update(setmeal);
        Long id = setmealDTO.getId();
        //删除套餐和菜品关联关系,操作setmeal_dish表 执行delete
        setmealDishMapper.delete(Math.toIntExact(id));
        List<SetmealDish> setmealDishes = setmealDTO.getSetmealDishes();
        setmealDishes.forEach(setmealDish -> {
            setmealDish.setSetmealId(id);
        });
        setmealDishMapper.save(setmealDishes);
    }

    /**
     * 条件查询
     * @param setmeal
     * @return
     */
    public List<Setmeal> list(Setmeal setmeal) {
        List<Setmeal> list = setmealMapper.list(setmeal);
        return list;
    }

    /**
     * 根据id查询菜品选项
     * @param id
     * @return
     */
    public List<DishItemVO> getDishItemById(Long id) {
        return setmealMapper.getDishItemBySetmealId(id);
    }
}
